package com.sl.plat.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sl.common.ResponseData;
import com.sl.water.dto.BPatrolRecordStaDTO;
import com.sl.water.entity.BPatrolRecord;
import com.sl.water.service.AttachService;
import com.sl.water.service.BPatrolRecordService;
import com.sl.water.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/plat/patrol")
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@Tag(name = "预警-人工巡查", description = "提供人工巡查增、删、改、查、列表查询相关功能")
public class BPatrolRecordController {

    private final BPatrolRecordService bPatrolRecordService;
    private final AttachService attachService;

    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public ResponseData<Page<BPatrolRecord>> pageList(
            @Parameter(
                    description = "页码",
                    example = "1", required = true
            )
            @RequestParam(defaultValue = "1", required = true) int page,
            @Parameter(
                    description = "分页数量",
                    example = "10", required = true
            )
            @RequestParam(defaultValue = "10", required = true) int size,
            @Parameter(
                    description = "类型",
                    example = "语音", required = false
            )
            @RequestParam(required = false) String kind,
            @Parameter(
                    description = "状态（未处理、已处理）",
                    example = "未处理", required = false
            )
            @RequestParam(required = false) String state
    ) {
        Page<BPatrolRecord> bPatrolRecordPage = bPatrolRecordService.pageList(page, size, kind, state);
        return ResponseData.success(bPatrolRecordPage);
    }

    @GetMapping
    @Operation(summary = "列表")
    public ResponseData<List<BPatrolRecord>> getList(
            @Parameter(
                    description = "类型",
                    example = "语音"
            )
            @RequestParam(required = false) String kind) {
        return ResponseData.success(bPatrolRecordService.listByKind(kind));
    }

    @PostMapping
    @Operation(summary = "新增")
    public ResponseData save(@RequestBody BPatrolRecordIVO bPatrolRecordVO) {
        BPatrolRecord bPatrolRecord = BaseVO.convert(bPatrolRecordVO, BPatrolRecord.class);
        bPatrolRecord.setState("未处理");
        bPatrolRecordService.save(bPatrolRecord);
        return ResponseData.success(bPatrolRecord.getId());
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除")
    public ResponseData removeById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        bPatrolRecordService.removeById(id);
        return ResponseData.success("删除成功");
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改")
    public ResponseData updateById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id, @RequestBody BPatrolRecordIVO bPatrolRecordVO) {
        BPatrolRecord bPatrolRecord = BaseVO.convert(bPatrolRecordVO, BPatrolRecord.class);
        bPatrolRecord.setId(id);
        bPatrolRecordService.updateById(bPatrolRecord);
        return ResponseData.success("修改成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询")
    public ResponseData<BPatrolRecord> getById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        return ResponseData.success(bPatrolRecordService.getById(id));
    }

    @PutMapping("/updateState/{id}")
    @Operation(summary = "修改状态")
    public ResponseData updateState(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id,
            @Parameter(
                    description = "状态(未处理、已处理)",
                    required = true,
                    example = "已处理"
            )
            @RequestParam String state) {
        bPatrolRecordService.updateState(id, state);
        return ResponseData.success("修改成功");
    }

    @PostMapping("/attachUpload")
    @Operation(summary = "附件上传")
    @ApiResponse(responseCode = "200", description = "上传成功", content = @Content(schema = @Schema(implementation = AttachVO.class)))
    public ResponseEntity<?> upload(
            @Parameter(description = "文件", required = true)
            @RequestPart(required = true)
                    MultipartFile file) {
        return attachService.upload("patrol", file);
    }

    @GetMapping("/sta")
    @Operation(summary = "按类型统计未处理数量")
    public ResponseData<BPatrolRecordStaDTO> sta() {
        return ResponseData.success(bPatrolRecordService.sta());
    }

}